//
//  UserLoginViewController.m
//  FreshMember
//
//  Created by qyc on 2019/4/28.
//  Copyright © 2019年 qyc. All rights reserved.
//

#import "UserLoginViewController.h"

#import "HAHomeViewController.h"

#import "NavigationController.h"
#import "CustomTextField.h"
#import "CheckHelper.h"
#import "CommentTool.h"

@interface UserLoginViewController ()
@property (weak, nonatomic) IBOutlet UIView *smsView;
@property (weak, nonatomic) IBOutlet UIButton *smsButton;

@property (weak, nonatomic) IBOutlet CustomTextField *mobileOneTextField; // 验证码的手机号
@property (weak, nonatomic) IBOutlet CustomTextField *smsTextField;

@end

@implementation UserLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view from its nib.
}

// 点击获取验证码
- (IBAction)getSmsClick:(id)sender {
    
    if (![CheckHelper checkTelNumber:self.mobileOneTextField.text]) {
        [CustomProgressHUD showMessage:@"请输入正确手机号" time:0];
        return;
    }

    [[HAUserAppApi sharedApi] requestSendSmsWithMobile:self.mobileOneTextField.text completionHandler:^(id  _Nonnull successHandler) {

        //倒计时
        [CommentTool countDownWithButton:self.smsButton andTime:59];
    } errorHandler:^(id  _Nonnull errorHandler) {

    }];
}

// 点击登陆
- (IBAction)loginClick:(id)sender {
    
    if (![CheckHelper checkTelNumber:self.mobileOneTextField.text]) {
        [CustomProgressHUD showMessage:@"请输入正确手机号" time:0];
        return;
    }
    else if ([self.smsTextField.text isEqualToString:@""]) {
        [CustomProgressHUD showMessage:@"请输入验证码" time:0];
        return;
    }

    [[MBProgressHUDTool shareProgressHUD] showHUDAddedTo:self.view];

    [[HAUserAppApi sharedApi] requestUserLoginCodeWithMobile:self.mobileOneTextField.text code:self.smsTextField.text completionHandler:^(id  _Nonnull successHandler) {

        [[MBProgressHUDTool shareProgressHUD] hideHUD];

        NSDictionary *info = successHandler;
        [UserDefaultHelper setDict:info forKey:UserInfoKey];

        NavigationController *nav = [[NavigationController alloc] init];
        [UIApplication sharedApplication].keyWindow.rootViewController = nav;
        // 创建控制器
        HAHomeViewController *vc = [[HAHomeViewController alloc] init];
        [nav setViewControllers:@[vc] animated:YES];
        
    } errorHandler:^(id  _Nonnull errorHandler) {
         [[MBProgressHUDTool shareProgressHUD] hideHUD];
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
